<?php
/**
 *@作者:MissZhang
 *@邮箱:<787727147@qq.com>
 *@创建时间:2022/10/8 11:34
 *@说明:用户相关验证
 */

namespace app\api\validate;

use app\common\model\Users;

class UserValidate extends BaseValidate
{
    //验证规则
    protected $rule = [
        'mobile'         => 'require|checkMobile|unique:users',
        'bank_name'      => 'require|chs',
        'bank_card'      => 'require|regex:bank_card',
        'oldpassword'    => 'requireIf:sms,0|checkPass:password',
        'oldpaypwd'      => 'requireIf:sms,0|checkPass:paypwd',
        'password'       => 'require|min:6',
        'conpassword'    => 'require|confirm:password',
        'rekey'          => 'checkReKey',
        'zhifubao'       => 'require|checkZfb',
        'realname'       => 'require|chs',
        'realname_reg'   => 'require|chs',
        'code'           => 'requireIf:sms,1',
        'wechat_account' => 'require',
        'wechat_name'    => 'require',
    ];
    //错误信息
    protected $message = [
        'mobile.require'        => '请输入手机号码',
        'mobile.checkMobile'    => '手机号码格式有误',
        'mobile.unique'         => '手机号码已注册',
        'oldpassword.requireIf' => '请输入原密码',
        'oldpaypwd.requireIf'   => '请输入原密码',
        'password.require'      => '请输入密码',
        'password.min'          => '密码至少6位',
        'conpassword.require'   => '请输入确认密码',
        'conpassword.confirm'   => '两次密码输入不一致',
        'code.requireIf'        => '请输入验证码',
        'code.checkCode'        => '请输入验证码',
        'rekey.require'         => '请输入推荐人',
        'zhifubao.require'      => '请输入支付宝',
        'zhifubao.checkZfb'     => '支付宝格式有误',
        'realname.require'      => '请输入支付宝姓名',
        'realname.chs'          => '支付宝姓名只能为中文',
        'realname_reg.require'  => '请输入姓名',
        'realname_reg.chs'      => '姓名只能为中文',
        'bank_name.require'     => '请输入开户银行',
        'bank_name.chs'         => '开户银行只能为中文',
        'bank_card.require'     => '请输入银行账号',
        'bank_card.regex'       => '银行账号格式有误',
    ];

    //注册验证场景
    public function sceneReg()
    {
        return $this->only(['mobile', 'code', 'realname_reg', 'password', 'conpassword', 'rekey'])
            ->append('code', 'checkCode:regis_sms_enable');
    }

    //绑定、注册验证场景
    public function sceneBindReg()
    {
        return $this->only(['mobile', 'code', 'realname_reg', 'password', 'conpassword', 'rekey'])
            ->append('code', 'checkCode:reg_bind_mobile');
    }

    //账号密码登陆
    public function sceneLogin()
    {
        return $this->only(['mobile', 'password'])
            ->remove('mobile', 'unique')
            ->append('password', 'checkUser');
    }

    //找回密码验证场景
    public function sceneForget()
    {
        return $this->only(['mobile', 'code', 'password', 'conpassword'])
            ->remove('mobile', 'unique')
            ->append('mobile', 'checkReg')
            ->append('code', 'checkCode');
    }

    public function sceneInfo()
    {
        return $this->only(['realname', 'bank_name', 'bank_card', 'code'])
            ->append('code', 'checkCode:user_info_sms_enable');
    }

    public function scenePass()
    {
        return $this->only(['oldpassword', 'code', 'password', 'conpassword'])
            ->append('code', 'checkCode:edit_pwd_sms_enable');
    }

    public function sceneNoPaypwd()
    {
        return $this->only(['code', 'password', 'conpassword'])
            ->append('code', 'checkCode:edit_paypwd_sms_enable');
    }

    public function scenePaypwd()
    {
        return $this->only(['oldpaypwd', 'code', 'password', 'conpassword'])
            ->append('code', 'checkCode:edit_paypwd_sms_enable');
    }

    public function sceneZfb()
    {
        return $this->only(['zhifubao', 'realname']);
    }

    public function sceneWechat()
    {
        return $this->only(['wechat_account', 'wechat_name']);
    }

    //检查旧密码
    protected function checkPass($value, $rule, $data)
    {
        $user = Users::where('mobile', $data['mobile'])->find();
        if ($user[$rule] != encrypt($value)) {
            return '原密码不正确';
        }
        return true;
    }

    //检查推荐人
    protected function checkReKey($value, $rule, $data)
    {
        if (empty($value)) {
            return true;
        }
        $user = Users::where('user_id', $value)->find();
        if (empty($user)) {
            return '推荐人不存在';
        }
        if ($user['is_lock'] == 1) {
            return '推荐人状态异常';
        }
        if ($user['mobile'] == $data['mobile']) {
            return '推荐人不能为自己';
        }
        return true;
    }
}
